package org.makcro.sms.services.chat.web.client.widgets.main;

import org.makcro.sms.services.chat.web.client.AppEvents;

import com.extjs.gxt.ui.client.event.MenuEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.extjs.gxt.ui.client.widget.menu.MenuBar;
import com.extjs.gxt.ui.client.widget.menu.MenuBarItem;
import com.extjs.gxt.ui.client.widget.menu.MenuItem;
import com.extjs.gxt.ui.client.widget.menu.SeparatorMenuItem;

/**
 * Main menu of the chat application
 * 
 * @author makcro
 * 
 */
public class MainMenuBar extends MenuBar {
	// Application menu
	private MenuBarItem	menuBarItemApplication;
	private MenuItem	menuItemLogout;
	private MenuItem	menuItemSubscribers;

	// Bulk menu
	private MenuBarItem	menuBarItemBulk;
	private MenuItem	menuItemBulkSearch;
	private MenuItem	menuItemBulkSendMessage;

	public MainMenuBar() {
		// add application menu
		add( getMenuBarItemApplication() );
		// add bulk menu
		add( getMenuBarItemBulk() );
	}

	private MenuBarItem getMenuBarItemApplication() {
		if (menuBarItemApplication == null) {
			Menu menu = new Menu();
			menu.add( getMenuItemSubscribers() );
			menu.add( new SeparatorMenuItem() );
			menu.add( getMenuItemLogout() );
			
			menuBarItemApplication = new MenuBarItem( "Aplikacija", menu );
		}
		return menuBarItemApplication;
	}

	private MenuItem getMenuItemLogout() {
		if (menuItemLogout == null) {
			menuItemLogout = new MenuItem( "Odjava" );
			menuItemLogout.addSelectionListener( new SelectionListener<MenuEvent>() {
				public void componentSelected(MenuEvent ce) {
					Dispatcher.get().dispatch( AppEvents.LOGOUT_REQUESTED );
				}
			} );
		}
		return menuItemLogout;
	}

	public MenuItem getMenuItemSubscribers() {
		if (this.menuItemSubscribers == null) {
			this.menuItemSubscribers = new MenuItem( "Korisnici" );
			this.menuItemSubscribers.addSelectionListener( new SelectionListener<MenuEvent>() {

				@Override
				public void componentSelected(MenuEvent ce) {
					Dispatcher.get().dispatch( AppEvents.SUBSCRIBERS_TAB_REQUESTED );

				}
			} );
		}
		return menuItemSubscribers;
	}

	public MenuBarItem getMenuBarItemBulk() {
		if (this.menuBarItemBulk == null) {
			Menu menu = new Menu();
			menu.add( getMenuItemBulkSearch() );
			menu.add( getMenuItemBulkSendMessage() );
			this.menuBarItemBulk = new MenuBarItem( "Bulk", menu );
		}
		return menuBarItemBulk;
	}

	public MenuItem getMenuItemBulkSearch() {
		if (this.menuItemBulkSearch == null) {
			this.menuItemBulkSearch = new MenuItem( "Pretraga" );
			this.menuItemBulkSearch.addSelectionListener( new SelectionListener<MenuEvent>() {

				@Override
				public void componentSelected(MenuEvent ce) {
					Dispatcher.get().dispatch( AppEvents.BulkEvents.BULK_TAB_REQUESTED );
				}
			} );
		}
		return menuItemBulkSearch;
	}

	public MenuItem getMenuItemBulkSendMessage() {
		if (this.menuItemBulkSendMessage == null) {
			this.menuItemBulkSendMessage = new MenuItem( "Posalji bulk poruku" );
			this.menuItemBulkSendMessage.addSelectionListener( new SelectionListener<MenuEvent>() {

				@Override
				public void componentSelected(MenuEvent ce) {
					Dispatcher.get().dispatch( AppEvents.BulkEvents.SEND_BULK_REQUESTED );
				}
			} );
		}
		return menuItemBulkSendMessage;
	}
}
